//
//  UUFleetEditVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 25/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUFleetEditVC.h"
#import "UUHybridManager+CharterSupplier.h"
#import "UUFleetManager.h"
#import <UUFoundation/UUCityManager.h>
#import <UUFoundation/DKProgressHUD.h>
#import "UUPermissionManager.h"

@interface UUFleetEditVC () <UUFleetManagerObserver>

@end

@implementation UUFleetEditVC

- (void)viewDidLoad {
    if (!self.editable) {
        [GetFleetManager() addObserver:self];
    }

    [super viewDidLoad];
}

- (NSString *)name {
    return @"车队";
}

- (BOOL)isAdd {
    return self.fleetId == nil;
}

- (BOOL)canEdit {
    return [GetPermissionManager() checkPermission:UUPermissionFlagFleetReadWrite];
}

- (NSURLRequest *)editRequest {
    return GetHybridManager().fleetEditURLRequest;
}

- (void)configEditVC:(UUFleetEditVC *)editVC {
    editVC.fleetId = self.fleetId;
}

- (NSURLRequest *)detailRequest {
    return GetHybridManager().fleetDetailURLRequest;
}

- (NSDictionary *)previewFields {
    if (self.fleetId != nil) {
        UUFleetModel *fleet = [GetFleetManager() fetchFleetWithFleetId:self.fleetId];
        
        NSMutableArray<NSString *> *cityNames = [NSMutableArray new];
        NSArray<NSString *> *districts = [fleet.districtIds componentsSeparatedByString:@","];
        for (NSString *district in districts) {
            [cityNames addObject:[GetCityManager() fetchCityWithCityId:@([district integerValue])].simpleName];
        }
        return @{
                 @"id" : self.fleetId,
                 @"district_names" : [cityNames componentsJoinedByString:@","],
                 @"district_ids" : fleet.districtIds ?: @"",
                 @"name" : fleet.name,
                 @"contact" : fleet.contact,
                 @"contact_mobile" : fleet.contactMobile,
                 };
    } else {
        return nil;
    }
}

- (void)addOrUpdate:(NSDictionary *)data {
    NSString *districtIds = data[@"district_ids"];
    NSString *name = data[@"name"];
    NSString *contact = data[@"contact"];
    NSString *contactMobile = data[@"contact_mobile"];
    
    WEAK_SELF();
    void (^completeBlock)(NSError *error) = ^(NSError *error) {
        HideTipsOnView(weakSelf.view);
        
        if (error) {
            ShowTipsOnView(error.localizedDescription, weakSelf.view);
        } else {
            NSString *tips = [NSString stringWithFormat:@"%@成功", weakSelf.fleetId == nil ? @"添加" : @"更新"];
            ShowTipsInKeyWindow(tips);
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    };
    
    ShowIndicator();
    
    if (self.fleetId == nil) {
        [GetFleetManager() addFleetWithDistrictIds:districtIds
                                              name:name
                                           contact:contact
                                     contactMobile:contactMobile
                                     completeBlock:completeBlock];
    } else {
        [GetFleetManager() updateFleetWithFleetId:self.fleetId
                                      districtIds:districtIds
                                             name:name
                                          contact:contact
                                    contactMobile:contactMobile
                                    completeBlock:completeBlock];
    }
}

#pragma mark - UUFleetManagerObserver methods

- (void)fleetManagerDidUpdateFleets {
    if (self.fleetId != nil) {
        [self reloadForm];
    }
}

@end
